package org.frxm.myencrypted;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.frxm.utilslibrary.AES;
import org.frxm.utilslibrary.BASE64;
import org.frxm.utilslibrary.DES;
import org.frxm.utilslibrary.MD5;
import org.frxm.utilslibrary.RSA;
import org.frxm.utilslibrary.SHA;

import java.security.Key;

public class MainActivity extends AppCompatActivity {

    private EditText et1, et2;
    private TextView tv;
    private String res;//记录可以被解密的密文
    private Key key1,key2,privateKey,publicKey;//记录对应的密钥
    private byte[] sign;//记录RSA的签名内容
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        et1 = (EditText) findViewById(R.id.et_1);
        et2 = (EditText) findViewById(R.id.et_2);
        tv = (TextView) findViewById(R.id.tv);
        key1= DES.initKey();
        key2= AES.iniKey();
        RSA.initKey();
        publicKey=RSA.getPublicKey();
        privateKey=RSA.getPrivateKey();
    }

    public void click(View v) {
        String msg=et1.getText().toString();
        switch (v.getId()) {
            case R.id.btn_1://MD5
                res=null;
                tv.setText(tv.getText()+"\r\nMD5加密："+ BASE64.encryptBase64Str(
                        MD5.encryptMD5(msg.getBytes())));
                break;
            case R.id.btn_2://Base64
                if(res==null){//加密
                    res=BASE64.encryptBase64Str(msg.getBytes());
                    tv.setText(tv.getText()+"\r\nBase64编码："+res);
                }else {
                    et2.setText(new String(BASE64.decodeBase64(res)));
                    res=null;
                }
                break;
            case R.id.btn_3://DES
                if(res==null){//加密
                    Log.e("TS", "click: "+BASE64.encryptBase64Str(key1.getEncoded()));
                    res=BASE64.encryptBase64Str(DES.encryptDES(key1,msg.getBytes()));
                    tv.setText(tv.getText()+"\r\nDES加密："+res);
                }else {
                    et2.setText(new String(DES.decodeDES(key1,BASE64.decodeBase64(res))));
                    res=null;
                }
                break;
            case R.id.btn_4://AES
                if(res==null){//加密
                    res=BASE64.encryptBase64Str(AES.encryptAES(key2,msg.getBytes()));
                    tv.setText(tv.getText()+"\r\nAES加密："+res);
                }else {
                    et2.setText(new String(AES.decodeAES(key2,BASE64.decodeBase64(res))));
                    res=null;
                }
                break;
            case R.id.btn_5://RSA加密
                if(res==null){//加密-私钥
                    byte[] data=RSA.encryptPrivateRSA(privateKey,msg.getBytes());
                    sign=RSA.sign(privateKey,data);
                    res=BASE64.encryptBase64Str(data);
                    tv.setText(tv.getText()+"\r\nRSA加密："+res);
                }else {//解密-公钥
                    byte[] val=BASE64.decodeBase64(res);
                    //验证公钥是否可用
                    if(RSA.verifySign(publicKey,sign,val)){
                        et2.setText(new String(RSA.decodePublicRSA(publicKey,val)));
                    }
                }
                break;
            case R.id.btn_6://SHA
                tv.setText(tv.getText()+"\r\nSHA加密："+ SHA.encryptedSHAToStr("SHA-1",msg.getBytes()));
                res=null;
                break;
        }
    }
}
